/**
 * Created by Administrator on 2017/4/14.
 */
"use strict";
swan_pc
    .controller("OrgOppClassifyController",function($scope,$rootScope,$state,ngDialog,oppBase,oppInfoSave,tagSetByOrg) {
        $scope.areaList=[
            {
                name:'华北',
                value:1
            },
            {
                name:'东北',
                value:2
            },
            {
                name:'华南',
                value:3
            },
            {
                name:'西北',
                value:4
            },
            {
                name:'西部',
                value:5
            },
            {
                name:'中部',
                value:6
            },
            {
                name:'华东',
                value:7
            }
        ];
        $scope.selectRegion='';

        $scope.presentInfo = $scope.$parent.presentInfo;
        console.log($scope.presentInfo);

        $scope.getAll=function () {
            oppBase.get({id:$rootScope.opp.id},function (result) {
                $scope.presentInfo = result;
                // console.log("INFO",result);
                $scope.getAllTagSet();
                $scope.getAddress();
            })
        };
        //$scope.getAll();


        $scope.getAllTagSet=function(){
            $scope.allTagSet=[];
            tagSetByOrg.query({id:$rootScope.organizationId},function(result){
                $scope.allTagSet=result;
                // console.log("TAG SET:",result);
                angular.forEach(result,function (item) {
                    angular.forEach(item.tagList,function (x) {
                        _.forEach($scope.presentInfo.tags,function (t) {
                            if (t.id==x.id){
                                x.$$checked=true;
                            }
                        });
                    })
                });
                // console.log($scope.allTagSet);
            });
        };
        $scope.getAllTagSet();

        $scope.addTag=function () {
            ngDialog.open({
                templateUrl:'scripts/components/organizationBusiness/myOpp/selectTag.html',
                scope:$scope,
                controller:['$scope',function ($scope) {
                    $scope.save=function(){
                        $scope.saveTag();
                        ngDialog.closeAll();
                    }
                    $scope.cancel=function(){
                        ngDialog.closeAll();
                    }
                }]
            })
        }

        $scope.saveTag=function () {
            $scope.presentInfo.tags=[];
            _.forEach($scope.allTagSet,function (item) {
                _.forEach(item.tagList,function (x) {
                    if (x.$$checked){
                        $scope.presentInfo.tags.push(x);
                    }
                })
            });
            $scope.saveChange();
        };

        $scope.saveChange=function () {
            // console.log("BEFORE",$scope.presentInfo);
            oppInfoSave.update($scope.presentInfo,function (result) {
                console.log("SAVED",result);
                $scope.showAdd=false;
            },function (result) {
                console.log("SAVE FAIL",result);
            })
        };

        $scope.$watch('presentInfo.province+presentInfo.city+presentInfo.district+presentInfo.street',function (newValue,oldValue) {
            if (newValue!=oldValue){
                $scope.showAdd=true;
            }
        });

    })